package cn.micro.keep.accounts.mapper;

import cn.micro.keep.accounts.entity.AccountDimUser;
import cn.micro.keep.accounts.entity.vo.UserCommonStatVO;
import cn.micro.keep.accounts.entity.vo.UserDetailVO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;

/**
 * <p>
 * 用户信息表 Mapper 接口
 * </p>
 *
 * @author 柘阳科技
 * @since 2023-11-08
 */
public interface AccountDimUserMapper extends BaseMapper<AccountDimUser> {

    @Select("select adu.* from account_dim_user adu\n" +
            "    left join account_dim_role_user adru on adu.id = adru.user_id\n" +
            "    left join account_dim_role adr on adru.role_id = adr.id where adr.code = 'MEMBER_ROLE' and (adu.username = #{value}  or adu.phone = #{value}  or adu.wx_openid = #{value} )")
    AccountDimUser queryByUsernameOrPhoneOrOpenId(String value);

    @Select("select id,username,wx_openid,name,phone,email,desc_info,birthday,gender,personal_signature,status,create_time,update_time\n" +
            "from account_dim_user where id = #{userId} ")
    UserDetailVO queryById(Long userId);

    @Select("select count(distinct aor.id) as account_record_total_num,count(distinct use_date) as check_in_days,count(distinct adsg.id) as group_num,sum(aor.amount) as account_balance  from account_dim_user adu\n" +
            "    left join account_dim_share_group_user adsgu on adu.id = adsgu.user_id\n" +
            "    left join account_dim_share_group adsg on adsg.id = adsgu.share_group_id\n" +
            "    left join account_ods_record aor on aor.user_id = adu.id where aor.deleted = 0 and adu.id=#{userId} ")
    UserCommonStatVO queryCommonStat(Long userId);
}
